package com.siwei.eqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siwei.eqi.mapper.IndexCalculationSubTaskMapper;
import com.siwei.eqi.pojo.entity.GuangdongLandUse;
import com.siwei.eqi.pojo.entity.IndexCalculationSubTaskEntity;
import com.siwei.eqi.pojo.entity.LandDataEntity;
import com.siwei.eqi.service.IndexCalculationSubTaskService;
import com.siwei.eqi.util.HqiCalculatorUtil;
import com.siwei.eqi.util.LandDataConverter;
import com.siwei.eqi.util.RegionBoundaryConverter;
import com.siwei.eqi.util.RegionUtil;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IndexCalculationSubTaskServiceImpl extends ServiceImpl<IndexCalculationSubTaskMapper, IndexCalculationSubTaskEntity> implements IndexCalculationSubTaskService {
    /**
     * 查询子任务详情
     *
     * @param entity
     * @return
     */
    @Override
    public R getList(IndexCalculationSubTaskEntity entity) throws Exception {
        QueryWrapper<IndexCalculationSubTaskEntity> wrapper = new QueryWrapper<>();
        if (entity != null) {
            wrapper.eq("parent_task_id", entity.getParentTaskId());
        }
        List<IndexCalculationSubTaskEntity> indexCalculationSubTaskEntities = baseMapper.selectList(wrapper);
        for (IndexCalculationSubTaskEntity indexCalculationSubTaskEntity : indexCalculationSubTaskEntities) {
            List<String> city = RegionUtil.getCity(indexCalculationSubTaskEntity.getCalculationArea());
            indexCalculationSubTaskEntity.setCalculationArea(city.get(0));
        }
        return R.ok(indexCalculationSubTaskEntities);
    }

    /**
     * 新增子任务
     *
     * @param indexCalculationSubTaskEntity 参数
     */
    @Override
    public void addIndexCalculationSubTask(IndexCalculationSubTaskEntity indexCalculationSubTaskEntity) throws Exception {
        IndexCalculationSubTaskEntity entity = new IndexCalculationSubTaskEntity();
        StringBuilder region = RegionUtil.getRegion(indexCalculationSubTaskEntity.getCalculationArea());
        List<Map<String, String>> regionBoundary = RegionUtil.getRegionBoundary(region);
        List<LandDataEntity> landDataEntities = RegionBoundaryConverter.convertToLandDataEntityList(regionBoundary);
        BigDecimal calculationResult = new BigDecimal(0);
//        for (LandDataEntity landDataEntity : landDataEntities) {
//            GuangdongLandUse guangdongLandUse = LandDataConverter.convertToGuangdongLandUse(landDataEntity);
//            BigDecimal bigDecimal = HqiCalculatorUtil.calculateHQI(guangdongLandUse);
//            if (bigDecimal.compareTo(BigDecimal.ZERO) != 0) {
//                calculationResult = calculationResult.add(bigDecimal);
//            }
//        }
        String[] split = indexCalculationSubTaskEntity.getCalculationArea().split(",");
        for (String s : split) {
            entity.setParentTaskId(indexCalculationSubTaskEntity.getParentTaskId());
            entity.setCalculationArea(s);
            entity.setCompletionPercentage("0/" + landDataEntities.size());
            entity.setCalculationStatus("计算中");
            entity.setCalculationResult(calculationResult);
            entity.setCreatedAt(new Date());
            entity.setUpdatedAt(new Date());
            baseMapper.insert(entity);
        }


    }
}
